package com.c2b.algorithm.newcoder.double_pointer;

/**
 * <a href="https://www.nowcoder.com/practice/c3a6afee325e472386a1c4eb1ef987f3?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj">反转字符串</a>
 * <p>写出一个程序，接受一个字符串，然后输出该字符串反转后的字符串。（字符串长度不超过1000）</p>
 * <p>
 * 数据范围：0≤n≤1000<br>
 * 要求：空间复杂度O(n)，时间复杂度O(n)
 * </p>
 *
 * @author c2b
 * @since 2023/3/15 15:01
 */
public class BM0091Solve_S {

    public String solve(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }
        char[] chars = str.toCharArray();
        int leftIndex = 0;
        int rightIndex = str.length() - 1;
        while (leftIndex < rightIndex) {
            // 做字符的交换
            char temp = chars[leftIndex];
            chars[leftIndex] = chars[rightIndex];
            chars[rightIndex] = temp;
            leftIndex++;
            rightIndex--;
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        BM0091Solve_S bm0091Solve_s = new BM0091Solve_S();
        System.out.println(bm0091Solve_s.solve("abcd"));
    }
}
